// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import ItemCell from "./ItemCell";
import YFDrawCallView from "./YFDrawCallView";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Yangfan extends cc.Component {

    @property({ type: YFDrawCallView })
    drawView: YFDrawCallView

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    @property(cc.Node)
    contentView: cc.Node = null

    @property(cc.Node)
    itemCell: cc.Node = null


    row = 0

    listNode = []
    lastNdoe: cc.Node;

    @property(cc.Camera)
    camera: cc.Camera  = null ;
    @property(cc.Label)
    draw: cc.Label = null;

    
    protected onLoad(): void {
      // cc.dynamicAtlasManager.showDebug(true)
        // 监听每一帧绘制结束后的事件
        cc.director.on(cc.Director.EVENT_AFTER_DRAW, () => {
            // 获取 draw call 的数量
            const drawCalls = cc.renderer.drawCalls;
            this.draw.string = 'draw call:'+drawCalls
        });


    }
    start() {
        for (let index = 0; index < 10; index++) {
            this.AddItem();
        }
      //  this.drawView.ReloadAll()
        //默认选中第一个
        // this.lastNdoe = this.drawView.FindNode('0', '');
        // this.ClickCell(this.lastNdoe)
    }


    ClickCell(select: cc.Node) {
        let selectNode = select
        let lastNdoe = this.lastNdoe
        if (lastNdoe) {
            this.drawView.opacity(lastNdoe.name, 'select', 0)
        }
        this.lastNdoe = this.drawView.FindNode(selectNode.name, '')
       // console.log(this.lastNdoe)
        if (this.lastNdoe) {
            this.drawView.opacity(this.lastNdoe.name, 'select', 255)
        }
    }

   async AddItem() {
        let cell: ItemCell = cc.instantiate(this.itemCell).getComponent(ItemCell)
        cell.node.name = '' + this.row++;
        cell.label.string = '序号' + cell.node.name
        this.contentView.addChild(cell.node)
        this.listNode.push(cell.node);
        cell.node.on('click', () => {
            //cell.label.string = 'fffff'
            this.ClickCell(cell.node);
        })

       // this.drawView.ReloadAll()
    // console.log('刷新');

    }
    AddItemMore() {
        for (let index = 0; index < 50; index++) {
            this.AddItem();
        }
        console.log(this.listNode.length);
        // this.drawView.ReloadAll()
        // console.log('刷新');

    }

     op = 255;
     ac = true
    RemoveItem() {
        let row = 1
        let node: cc.Node = this.listNode[row]
        this.listNode.splice(row, 1)
        this.drawView.RemoveItemNode(node.name);
        

        // this.op =  this.op === 255 ? 0 : 255
        // this.ac = this.ac === true ? false : true
        // this.drawView.ReloadAllPosCeShi(this.ac )
       
    }

    Reload() {
        // this.drawView.ReloadAllPos();
    }

    HideAction() {
        this.drawView.ReloadAll();
    }

    // protected update(dt: number): void {
    //     const drawCalls = cc.renderer.drawCalls;
    //     console.log(`Draw Calls: ${drawCalls}`);
    // }
}


